<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Cookie;
class Login extends Controller
{
    //登录
    public function index(){
        return $this->fetch('login/index');
    }
    //检车登录信息
    public function check()
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
        if(!trim($username)) {
            return show(0,'用户名不能为空');
        }
        if(!trim($password)) {
            return show(0,'密码不能为空');
        }
        if(!in_array($username,array('lis','5709','5504','5265','5174','5686','92101','5771','wc','5439','30802','5273'))){
            return show('0','仅支持客服或者系统管理员账户登录');
        }
        $obj = new \app\index\model\Admin();
        $userinfo = $obj->getAdminByUsername($username);
        if($userinfo){
            if($userinfo['logid'] == $username && $userinfo['password'] ==$password ) {
                /*if (in_array($username, $userinfo)) {*/
                //记录session
                Session::set('userinfo',$userinfo);
                //记录 cookie
                Cookie::set('userinfo',$userinfo['username'],342800);
                //登录成功 保存到log
                Model('Log')->addLog("登录成功");
                return show(1, '登录成功！');
            }else{
                return show(0,'用户或者密码错误');
            }
        }
        return show(0,'用户或者密码不存在');
    }

    //退出登录，清除session
    public function logout() {
        /*Model('Log')->addLog("退出登录",0);*/
        Model('Log')->addLog("退出登录", 0, 'logout');
        Session::clear();
        Cookie::delete('userinfo');
        $this->redirect('login/index');
    }

}